package com.ccc.boot.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccc.boot.entity.SysUserOnline;
import com.ccc.boot.monitor.mapper.SysUserOnlineMapper;
import com.ccc.boot.monitor.service.SysUserOnlineService;
import com.ccc.boot.pojo.Query;
import com.ccc.boot.security.cache.TokenCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author superC
 * @version 0.1.0
 * @date 2022/4/25
 */
@Service
public class SysUserOnlineServiceImpl implements SysUserOnlineService {

    @Autowired
    private SysUserOnlineMapper sysUserOnlineMapper;

    @Autowired
    private TokenCache tokenCache;

    @Override
    public IPage<SysUserOnline> page(Query query) {
        Page<SysUserOnline> page = new Page<>(query.getCurrent(), query.getSize());
        LambdaQueryWrapper<SysUserOnline> queryWrapper = new LambdaQueryWrapper<>();
        query.forEach((column, value) -> {
            if ("condition".equals(column)) {
                queryWrapper.and(wrapper -> {
                    wrapper.like(SysUserOnline::getRealName, value)
                            .or().like(SysUserOnline::getIp, value)
                            .or().like(SysUserOnline::getAddress, value);
                });
            }
        });
        queryWrapper.orderByDesc(SysUserOnline::getLoginTime);
        return sysUserOnlineMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean forcedReturn(String tokenKey) {
        // 删除redis的token
        tokenCache.deleteToken(tokenKey);

        // 删除数据
        sysUserOnlineMapper.deleteById(tokenKey);

        return true;
    }

}